package com.shanghaionstar.home.model;

import android.view.View;

import com.google.gson.Gson;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.vehicle.entity.ChargeParams;
import com.onstar.cn.vehicle.entity.ChargeTimeEnd;
import com.onstar.cn.vehicle.entity.DiagnosticElement;
import com.onstar.cn.vehicle.response.DiagnosticResponse;
import com.shanghaionstar.R;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * 不做逻辑处理，只对返回的json生成所需的展示类
 * Created by 6335yz on 2016/7/5.
 */
public class DataRefreshParser {
    DiagnosticElement leftFrontData = null, rightFrontData = null, leftRearData = null, rightRearData = null, placardFront = null, placardRear = null;

/*    private int getTireStatusId(String status) {
        if (status == null) {
            return 0;
        }
        if (status.toLowerCase(Locale.CHINA).equals("red")) {
            return 0;
        } else if (status.toLowerCase(Locale.CHINA).equals("yellow")) {
            return 0;
        } else {
            return 0;
        }
    }*/

    private int getTireStatusId(String status) {
        if (leftFrontData != null) {
            if (status == null) {
                return R.drawable.carmanage_tire_left_up_red;
            }
            if (status.toLowerCase(Locale.CHINA).equals("red")) {
                return R.drawable.carmanage_tire_left_up_red;
            } else if (status.toLowerCase(Locale.CHINA).equals("yellow")) {
                return R.drawable.carmanage_tire_left_up_yellow;
            } else {
                return R.drawable.carmanage_tire_left_up_blue;
            }
        }
        if (leftRearData != null) {
            if (status == null) {
                return R.drawable.carmanage_tire_left_down_red;
            }
            if (status.toLowerCase(Locale.CHINA).equals("red")) {
                return R.drawable.carmanage_tire_left_down_red;
            } else if (status.toLowerCase(Locale.CHINA).equals("yellow")) {
                return R.drawable.carmanage_tire_left_down_yellow;
            } else {
                return R.drawable.carmanage_tire_left_down_blue;
            }
        }
        if (rightFrontData != null) {
            if (status == null) {
                return R.drawable.carmanage_tire_right_up_red;
            }
            if (status.toLowerCase(Locale.CHINA).equals("red")) {
                return R.drawable.carmanage_tire_right_up_red;
            } else if (status.toLowerCase(Locale.CHINA).equals("yellow")) {
                return R.drawable.carmanage_tire_right_down_yellow;
            } else {
                return R.drawable.carmanage_tire_right_down_blue;
            }
        }
        if (rightRearData != null) {
            if (status == null) {
                return R.drawable.carmanage_tire_right_down_red;
            }
            if (status.toLowerCase(Locale.CHINA).equals("red")) {
                return R.drawable.carmanage_tire_right_down_red;
            } else if (status.toLowerCase(Locale.CHINA).equals("yellow")) {
                return R.drawable.carmanage_tire_right_down_yellow;
            } else {
                return R.drawable.carmanage_tire_right_down_blue;
            }
        }

        return 0;
    }

    public HomeModel dataRefreshHomeModel(String json, String completeTime) {
        DiagnosticResponses diagnosticResponses = new Gson().fromJson(json, DiagnosticResponses.class);
        if (diagnosticResponses == null) return null;
        List<DiagnosticResponse> diagnosticResponse = diagnosticResponses.getDiagnosticResponse();
        HomeModel homeModel = new HomeModel();
        homeModel.showTime = completeTime;
        if (diagnosticResponse != null && diagnosticResponse.size() > 0) {
            for (int k = 0; k < diagnosticResponse.size(); k++) {
                // ///////////////////////////////////////
                // 初始化油表的指针以及汽车的各个信息////////////////////////////
                // 机油余量
                if (diagnosticResponse.get(k).getName().equals(Constants.OIL_LIFE)) {
                    if (diagnosticResponse.get(k).getDiagnosticElement() != null) {
                        DiagnosticElement oil_life = diagnosticResponse.get(k).getDiagnosticElement().get(0);
                        if (oil_life.getValue() != null && !oil_life.getValue().trim().equals("")) {
                            if (CommonUtil.isNumerice(oil_life.getValue())) {
                                homeModel.oilLife = Math.round(Float.valueOf(oil_life.getValue()));
                                homeModel.tv_oil_value = String.valueOf(Math.round(Float.valueOf(oil_life.getValue())));
                            }
                        }
                    }
                }
                // 总行驶里程
                if (diagnosticResponse.get(k).getName().equals(Constants.ODOMETER)) {
                    if (diagnosticResponse.get(k).getDiagnosticElement() != null) {
                        DiagnosticElement odometer = diagnosticResponse.get(k).getDiagnosticElement().get(0);
                        if (odometer.getValue() != null && !odometer.getValue().trim().equals("")) {
                            if (CommonUtil.isNumerice(odometer.getValue())) {
                                homeModel.tv_mile_value = String.valueOf((Float.valueOf(odometer.getValue())).intValue());
                            }
                        }
                    }
                }

                if (diagnosticResponse.get(k).getName().equals(Constants.FUEL_TANK_INFO)) {
                    List<DiagnosticElement> fuel_tank_info = diagnosticResponse.get(k).getDiagnosticElement();
                    if (fuel_tank_info != null) {
                        // 燃油余量(百分比)
                        DiagnosticElement fuel_level = null;
                        int fuelTankInfoSize = fuel_tank_info.size();
                        for (int i = 0; i < fuelTankInfoSize; i++) {
                            DiagnosticElement temp = fuel_tank_info.get(i);
                            if (temp.getName().equals(Constants.FUEL_LEVEL)) {
                                fuel_level = temp;
                                break;
                            }
                        }
                        if (fuel_level != null) {
                            if (fuel_level.getValue() != null && !fuel_level.getValue().trim().equals("")) {
                                if (CommonUtil.isNumerice(fuel_level.getValue())) {
                                    float fuel_value = Float.valueOf(fuel_level.getValue());
                                    int value = Math.round(fuel_value);
                                    fuel_value = fuel_value <= 0 ? 0 : fuel_value;
                                    homeModel.fuel_value = fuel_value;
                                    homeModel.fuelTankInfo = value;
                                }
                            }
                        }
                    }
                }

                // 剩余燃油可行使里程(公里)
                if (diagnosticResponse.get(k).getName().equals(Constants.VEHICLE_RANGE)) {
                    if (diagnosticResponse.get(k).getDiagnosticElement() != null) {
                        DiagnosticElement fuel_tank_info = null;
                        int vehicleDatasSize = diagnosticResponse.get(k).getDiagnosticElement().size();
                        for (int i = 0; i < vehicleDatasSize; i++) {
                            DiagnosticElement temp = diagnosticResponse.get(k).getDiagnosticElement().get(i);
                            if (temp.getName().equals(Constants.GAS_RANGE)) {
                                fuel_tank_info = temp;
                            } else if (temp.getName().equals(Constants.TOTAL_RANGE) && !CommonUtil.isNull(temp.getValue())) {
                                if (CommonUtil.isNumerice(temp.getValue())) {
                                    homeModel.tv_total_mile_value = String.valueOf((int) Math.rint(Double.valueOf(temp.getValue())));
                                }
                            } else if (temp.getName().equals(Constants.EV_RANGE) && !CommonUtil.isNull(temp.getValue())) {
                                if (CommonUtil.isNumerice(temp.getValue())) {
                                    homeModel.tv_ev_mile_value = String.valueOf((int) Math.rint(Double.valueOf(temp.getValue())));
                                }
                            }
                        }
                    }
                }

                // //////////////////////////////////////
                // 初始化车的轮胎//////////////////////////////////////
                if (diagnosticResponse.get(k).getName().equals(Constants.TIRE_PRESSURE)) {
                    List<DiagnosticElement> tirePressureSet = diagnosticResponse.get(k).getDiagnosticElement();
                    if (tirePressureSet != null) {
                        for (int i = 0; i < tirePressureSet.size(); i++) {
                            DiagnosticElement vehicleData = tirePressureSet.get(i);
                            String name = vehicleData.getName();
                            if (name.equals(Constants.TIRE_PRESSURE_LF)) {
                                leftFrontData = vehicleData;
                            } else if (name.equals(Constants.TIRE_PRESSURE_LR)) {
                                leftRearData = vehicleData;
                            } else if (name.equals(Constants.TIRE_PRESSURE_RF)) {
                                rightFrontData = vehicleData;
                            } else if (name.equals(Constants.TIRE_PRESSURE_RR)) {
                                rightRearData = vehicleData;
                            } else if (name.equals(Constants.TIRE_PRESSURE_PLACARD_FRONT)) {
                                placardFront = vehicleData;
                            } else if (name.equals(Constants.TIRE_PRESSURE_PLACARD_REAR)) {
                                placardRear = vehicleData;
                            }
                        }

                        // 左前
                        if (leftFrontData != null) {
                            if (leftFrontData.getValue() != null && !leftFrontData.getValue().trim().equals("") && CommonUtil.isNumerice(leftFrontData.getValue())) {
                                homeModel.tv_front_left_value = String.valueOf(Math.round(Float.valueOf(leftFrontData.getValue())));
                            }
                            homeModel.img_red_left_front_visible = View.VISIBLE;
                            homeModel.img_red_left_front_res = getTireStatusId(leftFrontData.getMessage());
                            homeModel.leftFront = leftFrontData.getMessage();
                        }

                        // 右前
                        if (rightFrontData != null) {
                            if (rightFrontData.getValue() != null && !rightFrontData.getValue().trim().equals("") && CommonUtil.isNumerice(rightFrontData.getValue())) {
                                homeModel.tv_front_right_value = String.valueOf(Math.round(Float.valueOf(rightFrontData.getValue())));
                            }
                            homeModel.img_red_right_front_visible = View.VISIBLE;
                            homeModel.img_red_right_front_res = getTireStatusId(rightFrontData.getMessage());
                            homeModel.rightFront = rightFrontData.getMessage();
                        }

                        // 左后
                        if (leftRearData != null) {
                            if (leftRearData.getValue() != null && !leftRearData.getValue().trim().equals("") && CommonUtil.isNumerice(leftRearData.getValue())) {
                                homeModel.tv_rear_left_value = String.valueOf(Math.round(Float.valueOf(leftRearData.getValue())));
                            }
                            homeModel.img_red_left_rear_visible = View.VISIBLE;
                            homeModel.img_red_left_rear_res = getTireStatusId(leftRearData.getMessage());
                            homeModel.leftRear = leftRearData.getMessage();
                        }

                        // 右后
                        if (rightRearData != null) {
                            if (rightRearData.getValue() != null && !rightRearData.getValue().trim().equals("") && CommonUtil.isNumerice(rightRearData.getValue())) {
                                homeModel.tv_rear_right_value = String.valueOf(Math.round(Float.valueOf(rightRearData.getValue())));
                            }
                            homeModel.img_red_right_rear_visible = View.VISIBLE;
                            homeModel.img_red_right_rear_res = getTireStatusId(rightRearData.getMessage());
                            homeModel.rightRear = rightRearData.getMessage();
                        }

                        // 前胎参考
                        if (placardFront != null) {
                            if (placardFront.getValue() != null && !placardFront.getValue().trim().equals("") && CommonUtil.isNumerice(placardFront.getValue())) {
                                homeModel.tv_front_value = String.valueOf(Math.round(Float.valueOf(placardFront.getValue())));
                            }
                        }

                        // 后胎参考数据
                        if (placardRear != null) {
                            if (placardRear.getValue() != null && !placardRear.getValue().trim().equals("") && CommonUtil.isNumerice(placardRear.getValue())) {
                                homeModel.tv_rear_value = String.valueOf(Math.round(Float.valueOf(placardRear.getValue())));
                            }
                        }
                    }

                }
                loadPhevData(homeModel, diagnosticResponse, k);
            }
        }
        return homeModel;
    }

    private HomeModel loadPhevData(HomeModel homeModel, List<DiagnosticResponse> diagnosticResponse, int k) {
        ChargeParams chargeParams = new ChargeParams();
        // phev充电完成
        if (diagnosticResponse.get(k).getName().equals(Constants.EV_ESTIMATED_CHARGE_END)) {
            if (diagnosticResponse.get(k).getDiagnosticElement() != null) {
                Map<String, String> chargeFinishTime = new HashMap<String, String>();
                for (DiagnosticElement entityDiagnosticElement : diagnosticResponse.get(k).getDiagnosticElement()) {
                    ChargeTimeEnd chargeTimeEnd = ChargeTimeEnd.fromValue(entityDiagnosticElement.getName());
                    if (chargeTimeEnd == null) continue;
                    switch (chargeTimeEnd) {
                        case EST_CHG_END_120V:
                            chargeFinishTime.put(ChargeTimeEnd.EST_CHG_END_120V.getStringValue(), entityDiagnosticElement.getValue());
                            break;
                        case EST_CHG_END_240V:
                            chargeFinishTime.put(ChargeTimeEnd.EST_CHG_END_240V.getStringValue(), entityDiagnosticElement.getValue());
                            break;
                        case EST_CHG_END_400V:
                            chargeFinishTime.put(ChargeTimeEnd.EST_CHG_END_400V.getStringValue(), entityDiagnosticElement.getValue());
                            break;
                        default:
                            chargeFinishTime.put(ChargeTimeEnd.EST_CHG_END_240V.getStringValue(), entityDiagnosticElement.getValue());
                            break;
                    }
                }
                chargeParams.setChargeFinishTime(chargeFinishTime);
            }
        }

        // phev充电状态
        if (diagnosticResponse.get(k).getName().equals(Constants.EV_CHARGE_STATE)) {
            if (diagnosticResponse.get(k).getDiagnosticElement() != null) {
                DiagnosticElement ev_plug_state = diagnosticResponse.get(k).getDiagnosticElement().get(0);
                if (ev_plug_state.getValue() != null && !ev_plug_state.getValue().trim().equals("")) {
                    chargeParams.setCharginsState(ev_plug_state.getValue());
                }
            }
        }

        // phev充电电压
        if (diagnosticResponse.get(k).getName().equals(Constants.EV_PLUG_VOLTAGE)) {
            if (diagnosticResponse.get(k).getDiagnosticElement() != null) {
                DiagnosticElement ev_plug_volt = diagnosticResponse.get(k).getDiagnosticElement().get(0);
                if (ev_plug_volt.getValue() != null && !ev_plug_volt.getValue().trim().equals("")) {
                    chargeParams.setVolt(ev_plug_volt.getValue());
                }
            }
        }

        // phev充电完成百分比
        if (diagnosticResponse.get(k).getName().equals(Constants.EV_BATTERY_LEVEL)) {
            if (diagnosticResponse.get(k).getDiagnosticElement() != null) {
                DiagnosticElement charge_percent = diagnosticResponse.get(k).getDiagnosticElement().get(0);
                if (charge_percent.getValue() != null && !charge_percent.getValue().trim().equals("")) {
                    String batteryLevel = charge_percent.getValue();
                    homeModel.batteryLevel = batteryLevel;
                    if (CommonUtil.isNumerice(batteryLevel)) {
                        chargeParams.setChargePercent(batteryLevel);
                        homeModel.tv_battery_value = String.valueOf((int) Math.rint(Double.valueOf(batteryLevel)));
                    }
                }
            }
        }
        homeModel.chargeParams = chargeParams;
        return homeModel;
    }
}





